define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'personnel/index',
                    add_url: 'personnel/add',
                    edit_url: 'personnel/edit',
                    del_url: 'personnel/del',
                    multi_url: 'personnel/multi',
                    table: 'personnel',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'school.name', title: __('School.name')},
                        {field: 'grade.name', title: __('Grade.name')},
                        {field: 'classes.name', title: __('Classes.name')},
                        {field: 'role', title: __('Role'), searchList: {"1":__('Role 1'),"2":__('Role 2'),"3":__('Role 3'),"4":__('Role 4'),"5":__('Role 5'),"6":__('Role 6'),"7":__('Role 7'),"8":__('Role 8')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name')},
                        {field: 'sexlist', title: __('Sexlist'), searchList: {"1":__('Sexlist 1'),"2":__('Sexlist 2'),"3":__('Sexlist 3')}, formatter: Table.api.formatter.normal},
                        {field: 'age', title: __('Age')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'is_class_text', title: __('Is_class'), searchList: {"1":__('Is_class 1'),"2":__('Is_class 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $("#c-grade_id").data("params", function (obj) {
                return {custom: {school_id: $("#c-school_id").val()}};
            });
            $("#c-classes_id").data("params", function (obj) {
                return {custom: {grade_id: $("#c-grade_id").val()}};
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});